package cn.ito.zjgsu.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ito.zjgsu.domain.IndustryType;
import cn.ito.zjgsu.domain.IndustryTypeExample;
import cn.ito.zjgsu.domain.ProjectType;
import cn.ito.zjgsu.domain.ProjectTypeExample;
import cn.ito.zjgsu.domain.wrapper.IndustryTypeWrapper;
import cn.ito.zjgsu.domain.wrapper.ProjectTypeWrapper;
import cn.ito.zjgsu.service.IndustryTypeService;
import cn.ito.zjgsu.service.ProjectTypeService;
import cn.ito.zjgsu.utils.StatusCodeUtil;

@Controller
@RequestMapping("/admin")
public class ItypePtypeController {
	
	@Resource
	private ProjectTypeService projectTypeService;
	
	@Resource
	private IndustryTypeService industryTypeService;
	
	@Autowired
	private StatusCodeUtil statusCodeUtil;

	/*
	  新增项目类别 /admin/projectType/(POST)
	  删除项目类别/admin/projectType/<projectTypeId>(DELETE) 
	  修改项目类别 /admin/projectType/<projectTypeId>(PUT)
	  
	  新增行业类别 admin/industryType/(POST) 
	  删除行业类别 admin/ industryType/<industryTypeId>(DELETE) 
	  修改行业类别 admin/ industryType/< industryTypeId>(PUT)*/
	 

	private static Logger logger = Logger.getLogger(ItypePtypeController.class);

	@RequestMapping("/IPIndex")
	public String index() {
		return "sys/proMan";
	}

	// 查询所有项目类别
	@RequestMapping(value = "/projectType/projectTypes/{pageNum}/{records}", method = RequestMethod.GET)
	@ResponseBody
	public String queryProjectType(@PathVariable int pageNum,
			@PathVariable int records) {
		logger.info("查询所有项目类别");
		logger.info("pageNum=" + pageNum);
		logger.info("records=" + records);
		List<ProjectType> projectTypes = projectTypeService.queryByPage(pageNum,records);
		return ProjectTypeWrapper.toJsonArrayString(
				ProjectTypeWrapper.wrapList(projectTypes));
	}

	// 查询所有行业类别
	@RequestMapping(value = "/industryType/industryTypes/{pageNum}/{records}", method = RequestMethod.GET)
	@ResponseBody
	public String queryIndustyType(@PathVariable int pageNum,
			@PathVariable int records) {
		logger.info("查询所有行业类别");
		logger.info("pageNum=" + pageNum);
		logger.info("records=" + records);
		List<IndustryType> industryTypes = industryTypeService.queryByPage(pageNum , records);
		return IndustryTypeWrapper.toJsonArrayString(
				IndustryTypeWrapper.wrapList(industryTypes));
	}

	// 新增项目类别
	@RequestMapping(value = "/projectType/", method = RequestMethod.POST)
	@ResponseBody
	public String createProjectType(@RequestParam("projectTypeName") String typeName) {
		logger.info("新增项目类别");
		logger.info("typeName=" + typeName);
		ProjectType projectType = new ProjectType();
		projectType.setProjectTypeName(typeName);
		List<ProjectType> pts = new ArrayList<ProjectType>();
		pts.add(projectType);
		String status = projectTypeService.insertList(pts);
		return statusCodeUtil.toJsonStatus(status);
	}

	// 删除项目类别
	@RequestMapping(value = "/projectType", method = RequestMethod.DELETE)
	@ResponseBody
	public String deleteProjectType(@RequestParam("projectTypeIds") Integer[] projectTypeIds) {
		List<Integer> ptids = new ArrayList<Integer>(); 
		ptids.addAll(Arrays.asList(projectTypeIds));
		ProjectTypeExample projectExample = new ProjectTypeExample();
		projectExample.or().andProjectTypeIdIn(ptids);
		String status = projectTypeService.deleteProjectByExample(projectExample);
		return statusCodeUtil.toJsonStatus(status);
	}

	// 修改项目类别
	@RequestMapping(value = "/projectType/{projectTypeId}", method = RequestMethod.PUT)
	@ResponseBody
	public String updateProjectType(@PathVariable("projectTypeId") int projectTypeId,
			@RequestParam("projectTypeName") String typeName) {
		logger.info("修改项目类别");
		logger.info("projectTypeId=" + projectTypeId);
		logger.info("typeName=" + typeName);
		
		ProjectTypeExample projectExample = new ProjectTypeExample();
		projectExample.or().andProjectTypeIdEqualTo(projectTypeId);
		ProjectType projectType = new ProjectType();
		projectType.setProjectTypeName(typeName);
		String status = projectTypeService.updateProjectByExample(projectType , projectExample);
		
		return statusCodeUtil.toJsonStatus(status);
	}

	// 新增行业类别
	@RequestMapping(value = "/industryType/", method = RequestMethod.POST)
	@ResponseBody
	public String createIndustryType(@RequestParam("industryTypeName") String typeName) {
		logger.info("新增行业类别");
		logger.info("typeName=" + typeName);
		
		List<IndustryType> its = new ArrayList<IndustryType>();
		IndustryType industryType = new IndustryType();
		industryType.setIndustryTypeName(typeName);
		its.add(industryType);
		String status = industryTypeService.insertList(its);	
		return statusCodeUtil.toJsonStatus(status);
	}

	// 删除行业类别
	@RequestMapping(value = "/industryType", method = RequestMethod.DELETE)
	@ResponseBody
	public String deleteIndustryType(@RequestParam("industryTypeIds") Integer[] industryTypeIds) {
		logger.info("删除行业类别");
		logger.info("industryTypeId=" + industryTypeIds);
		IndustryTypeExample industryTypeExample = new IndustryTypeExample();
		if(null != industryTypeIds){
			industryTypeExample.or().andIndustryTypeIdIn(Arrays.asList(industryTypeIds));
		}
		String status = industryTypeService.deleteIndustryByExample(industryTypeExample);
		
		
		return statusCodeUtil.toJsonStatus(status);
	}

	// 修改行业类别
	@RequestMapping(value = "/industryType/{industryTypeId}", method = RequestMethod.PUT)
	@ResponseBody
	public String updateIndustryType(@PathVariable("industryTypeId") int industryTypeId,
			@RequestParam("industryTypeName") String typeName) {
		logger.info("修改行业类别");
		logger.info("industryTypeId=" + industryTypeId);
		logger.info("typeName=" + typeName);
		
		IndustryTypeExample industryTypeExample = new IndustryTypeExample();
		industryTypeExample.or().andIndustryTypeIdEqualTo(industryTypeId);
		IndustryType industryType = new IndustryType();
		industryType.setIndustryTypeName(typeName);
		String status = industryTypeService.updateByIndustryTypeExample(industryType,industryTypeExample);
		
		
		
		return statusCodeUtil.toJsonStatus(status);
	}

}
